package com.test.fileapplication.base;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;


import com.test.fileapplication.util.FileIconUtils;
import com.test.fileapplication.R;
import com.test.fileapplication.util.RemoteFile;
import com.test.fileapplication.util.StatusBarUtil;

import java.text.Collator;
import java.util.Comparator;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import butterknife.ButterKnife;

public abstract class BaseFragment extends Fragment implements View.OnClickListener {

    public static final int UN_UPLOAD = 0;
    public static final int UPLOADED = 1;
    public static final int ALL_UPLOAD = 2;

    private View mContentView;
    public Activity baseActivity;

    public Activity getbaseActivity() {
        return baseActivity;
    }

    public void setbaseActivity(Activity baseActivity) {
        this.baseActivity = baseActivity;
    }

    private Collator collator = Collator.getInstance(java.util.Locale.CHINA);

//    private RxPermissions rxPermissions;

    public static final int SORT_BY_NAME = 1;
    public static final int SORT_BY_TIME = 2;

    public static final int GRID_VIEW = 1;
    public static final int LIST_VIEW = 2;

    public int sortType = SORT_BY_NAME;

    public int getsortType() {
        return sortType;
    }

    public void setsortType(int sortType) {
        this.sortType = sortType;
    }

    public int viewType = GRID_VIEW;

    public int getviewType() {
        return viewType;
    }

    public void setviewType(int viewType) {
        this.viewType = viewType;
    }

    public boolean isSelectMode = false;

    public boolean getisSelectMode() {
        return isSelectMode;
    }

    public void setisSelectMode(boolean baseActivity) {
        this.isSelectMode = isSelectMode;
    }

    public static final int REQUEST_TARGET_PATH = 1;

    private boolean isFixed(RemoteFile file, String name) {
        if ("0".equals(file.getParentId()) && name.equals(file.getFileName()) && "2".equals(file.getCategory())) {
            return true;
        }
        return false;
    }


    /**
     * 先显示所有文件夹，再显示所有文件
     * 根目录下，葫芦备份>备份恢复>收到文件>我的收藏>我的相册
     */
    private Comparator<RemoteFile> sortByName = new Comparator<RemoteFile>() {
        @Override
        public int compare(RemoteFile o1, RemoteFile o2) {
            if (isFixed(o1, baseActivity.getResources().getString(R.string.back_up))) {
                return -1;
            }
            if (isFixed(o2, baseActivity.getResources().getString(R.string.back_up))) {
                return 1;
            }

            if (isFixed(o1, baseActivity.getResources().getString(R.string.back_up_revert))) {
                return -1;
            }
            if (isFixed(o2, baseActivity.getResources().getString(R.string.back_up_revert))) {
                return 1;
            }

            if (isFixed(o1, baseActivity.getResources().getString(R.string.received_file))) {
                return -1;
            }
            if (isFixed(o2, baseActivity.getResources().getString(R.string.received_file))) {
                return 1;
            }

            if (isFixed(o1, baseActivity.getResources().getString(R.string.my_collect))) {
                return -1;
            }
            if (isFixed(o2, baseActivity.getResources().getString(R.string.my_collect))) {
                return 1;
            }

            if (isFixed(o1, baseActivity.getResources().getString(R.string.my_photo_album))) {
                return -1;
            }
            if (isFixed(o2, baseActivity.getResources().getString(R.string.my_photo_album))) {
                return 1;
            }

            if (isFixed(o1, baseActivity.getResources().getString(R.string.shiminfankui))) {
                return -1;
            }
            if (isFixed(o2, baseActivity.getResources().getString(R.string.shiminfankui))) {
                return 1;
            }

            if ("2".equals(o1.getCategory())) {
                //o1是文件夹
                if ("2".equals(o2.getCategory())) {
                    //都是文件夹
                    return collator.compare(o1.getFileName(), o2.getFileName());
                } else {
                    return -1;
                }
            } else {
                //o1是文件
                if ("2".equals(o2.getCategory())) {
                    return 1;
                } else {
                    //都是文件
                    String o1Name = FileIconUtils.getFileName(o1.getFileName());
                    String o2Name = FileIconUtils.getFileName(o2.getFileName());
                    return collator.compare(o1Name, o2Name);
                }
            }
        }
    };

    public Comparator<RemoteFile> getsortByName() {
        return sortByName;
    }

    public void setsortByName(Comparator<RemoteFile> sortByName) {
        this.sortByName = sortByName;
    }

    @Override
    public void onAttach(@NonNull Context context) {
        super.onAttach(context);
        baseActivity = (Activity)context;
//        rxPermissions = new RxPermissions(baseActivity);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        mContentView = inflater.inflate(getLayoutResourceId(), container, false);
        ButterKnife.bind(this, mContentView);
        //add by houxiansheng 2019-12-5 17:40:23 动态修改StatusBar的字体颜色：修改顶部时间颜色为黑色
        StatusBarUtil.setStatusBar(baseActivity, false, false);
        init();
        setUpView();
        return mContentView;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        setUpData();
    }

    protected abstract int getLayoutResourceId();

    protected abstract void setUpView();

    protected abstract void setUpData();

    protected void init() {
    }



    //    public void startUpload(File originFile, String dirID) {
    //        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
    //            //需要前台服务
    //            rxPermissions.request(Manifest.permission.FOREGROUND_SERVICE)
    //                    .subscribe(new Observer<Boolean>() {
    //                        @Override
    //                        public void onSubscribe(Disposable d) {
    //
    //                        }
    //
    //                        @Override
    //                        public void onNext(Boolean aBoolean) {
    //                            if (aBoolean) {
    //                                upload(originFile, dirID);
    //                            } else {
    //                                ToastUtil.showCenterHasImageToast(baseActivity, "请先允许前台服务权限");
    //                            }
    //                        }
    //
    //                        @Override
    //                        public void onError(Throwable e) {
    //                            ToastUtil.showCenterHasImageToast(baseActivity, "申请前台服务异常");
    //                        }
    //
    //                        @Override
    //                        public void onComplete() {
    //
    //                        }
    //                    });
    //        } else {
    //            upload(originFile, dirID);
    //        }
    //    }
//
//    public interface DataCallBack {
//        void onReadFinished(List<VideoList> videos);
//    }

//    private LoadingDialog mLoadingDialog;

    /**
     * 显示加载对话框
     */
    public void showLoadingDialog(Context mContext) {
//        if (isAdded()) {
//            if (mLoadingDialog == null) {
//                mLoadingDialog = new LoadingDialog(mContext);
//            }
//            mLoadingDialog.resetDialog();
//            mLoadingDialog.setCancelable(true);
//            mLoadingDialog.setCanceledOnTouchOutside(false);
//            mLoadingDialog.show();
//        }
//        SVProgressHUD.show(getContext());

    }

    /**
     * 隐藏加载对话框
     */
    public void hideLoadingDialog() {
//        if (isAdded()) {
//            if ((mLoadingDialog != null) && mLoadingDialog.isShowing()) {
//                mLoadingDialog.dismiss();
//            }
//        }
//        if (SVProgressHUD.isShowing(getContext())) {
//            SVProgressHUD.dismiss(getContext());
//        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    //    public void newUpload(List<File> files, String dirID) {
    //        long total = 0;
    //        for (File f : files) {
    //            total += f.length();
    //        }
    //        final long needUse = total;
    //
    //        ApiRetrofit.getInstance()
    //                .postCapacity()
    //                .subscribeOn(Schedulers.io())
    //                .observeOn(AndroidSchedulers.mainThread())
    //                .subscribe(new Observer<CapacityResponseBean>() {
    //                    @Override
    //                    public void onSubscribe(Disposable d) {
    //
    //                    }
    //
    //                    @Override
    //                    public void onNext(CapacityResponseBean capacityResponseBean) {
    //                        if (capacityResponseBean.isSuccess()) {
    //                            Long magnetismUsedCapacity = Long.parseLong(capacityResponseBean.getData().getMagnetismUsedCapacity());
    //                            Long magnetismCapacity = Long.parseLong(capacityResponseBean.getData().getMagnetismCapacity());
    //                            long canUse = magnetismCapacity - magnetismUsedCapacity;
    //                            if (needUse >= canUse) {
    //                                ToastUtil.showCenterHasImageToast(baseActivity, "上传的文件大小超过剩余可用空间");
    //                                return;
    //                            } else {
    //                                for (File f : files) {
    //                                    if (f.exists()) {
    //                                        UploadConstants.uploadFile(baseActivity, f, dirID, BaseFragment.this);
    //                                    }
    //                                }
    //                            }
    //                        }
    //                    }
    //
    //                    @Override
    //                    public void onError(Throwable e) {
    //
    //                    }
    //
    //                    @Override
    //                    public void onComplete() {
    //
    //                    }
    //                });
    //    }
}
